﻿#ifndef _TOOLS_H_
#define _TOOLS_H_

#include "debug.h"
#include "stdbool.h"
#include "pktbuf.h"

/**
 * @brief 判断是大端还是小端存储
 */
static inline int is_little_endian()
{
    uint16_t num = 0x1234;
    return *(uint8_t *)&num == 0x34;
}

static inline uint16_t swap_u16(uint16_t v)
{
    uint16_t r = ((v & 0xff) << 8) | ((v >> 8) & 0xff);
    return r;
}

static inline uint32_t swap_u32(uint32_t v)
{
    uint16_t r = (((v >> 0) & 0xff) << 24) |
                 (((v >> 8) & 0xff) << 16) |
                 (((v >> 16) & 0xff) << 8) |
                 (((v >> 24) & 0xff) << 0);
    return r;
}

static int a = 1;

#if NET_ENDIAN_LITTLE
#define x_htons(v) swap_u16(v)
#define x_htonl(v) swap_u32(v)
#define x_ntohs(v) swap_u16(v)
#define x_ntohl(v) swap_u32(v)
#else
#define x_htons(v) v
#define x_htonl(v) v
#define x_ntohs(v) v
#define x_ntohl(v) v
#endif

net_err_t tools_init();
uint16_t tools_checksum16(uint32_t offset, const void *buf, uint16_t len, uint32_t pre_sum, bool complement);
uint16_t tools_checksum_peso(pktbuf_t *buf, const ipaddr_t *dst_ip, const ipaddr_t *src_ip, uint8_t protocol);

#endif